package sh.ds.el.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sh.ds.el.action.base.ElAction;
import sh.ds.el.common.Constants;
import sh.ds.el.common.SecurityUtils;
import sh.ds.el.entity.UserVO;
import sh.ds.el.service.MenuService;
import sh.ds.el.service.UserService;

@SuppressWarnings("serial")
@Controller("defaultAction")
@Scope("prototype")
public class DefaultAction extends ElAction {
	
	@Autowired
	private UserService userService;
	@Autowired
	private MenuService menuService;
	
	private UserVO userVo;

	public String login() throws Exception {
		if (userService.isLoginSuccess(userVo.getUserCode(), userVo.getPassword())) {
			UserVO currentUser = userService.queryUniqueUserByUserCode(userVo.getUserCode());
			SecurityUtils.keepCurrentUser(currentUser);
			
			getRequest().setAttribute("menuList", menuService.
					queryFirstMenusForUser(currentUser.getUserId()));
			
			return SUCCESS;
		}

		getRequest().setAttribute(Constants.COMMENT, "Username or Password is wrong");
		return ERROR;
	}
	
	public UserVO getUserVo() {
		return userVo;
	}

	public void setUserVo(UserVO userVo) {
		this.userVo = userVo;
	}
	
}
